package org.restorator.dao.implement;

import org.restorator.dao.base.OrderDao;
import org.restorator.dao.implement.engin.OrderEnricher;
import org.restorator.dao.implement.engin.OrderRowMapper;
import org.restorator.dao.jdbc.AbstractJdbsTemplate;
import org.restorator.dao.jdbc.Enricher;
import org.restorator.dao.jdbc.PreparedSetter;
import org.restorator.dao.jdbc.SimpleResultSetExtractor;
import org.restorator.dao.tx.TransactionManager;
import org.restorator.entity.Order;
import org.restorator.exception.DataStoreException;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Snisar Bogdan.
 */
public class OrderDaoImpl extends AbstractJdbsTemplate<Order, Integer> implements OrderDao {

    private static final String SQL_SELECT_ALL = "SELECT o.ORDER_ID, o.ORDER_OPEN_DATE, o.ORDER_CLOSE_DATE FROM ORDERS o"; //todo
    private static final String SQL_SELECT_BY_TABLE_ID = "SELECT o.ORDER_ID, o.ORDER_OPEN_DATE, o.ORDER_CLOSE_DATE " +
            "FROM ORDERS o WHERE o.Table_ID = ?";
    private static final String SQL_SELECT_BY_ID = "SELECT ORDER_ID, ORDER_OPEN_DATE, ORDER_CLOSE_DATE " +
            "FROM ORDERS o WHERE o.Order_ID = ?";
    private static final String SQL_INSERT = "INSERT INTO ORDERS (Table_ID, ORDER_OPEN_DATE) " +
            "VALUE (?, ?)";
    private static final String SQL_DELETE_FROM_ORDERS = "DELETE FROM ORDERS WHERE ORDER_ID = ?";
    private static final String SQL_DELETE_FROM_ORDER_ITEMS = "DELETE FROM ORDER_ITEM WHERE ORDER_ID = ?";

    public OrderDaoImpl() { }

    @Override
    public Order get(final Integer integer) throws DataStoreException {
        return executeParameter(SQL_SELECT_BY_ID,
                new SimpleResultSetExtractor(new OrderRowMapper()),
                new OrderEnricher(new ItemDaoImpl()),
                new PreparedSetter() {
            @Override
            public void setParameters(PreparedStatement ps) throws SQLException {
                ps.setInt(1, integer);
            }
        });
    }

    @Override
    public Order update(Order persistElement) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void delete(final Order deleted) throws DataStoreException {
        updateOne(SQL_DELETE_FROM_ORDER_ITEMS, deleted, Enricher.NULL, new PreparedSetter() {
            @Override
            public void setParameters(PreparedStatement ps) throws SQLException {
                ps.setInt(1, deleted.getOrderId());
            }
        });
        updateOne(SQL_DELETE_FROM_ORDERS, deleted,
                new OrderEnricher(new ItemDaoImpl()),
                new PreparedSetter() {
                    @Override
                    public void setParameters(PreparedStatement ps) throws SQLException {
                        ps.setInt(1, deleted.getOrderId());
                    }
                });
    }

    @Override
    public long count() {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Integer save(Order persist, final Object... addParams) throws DataStoreException {
        return executeInsert(SQL_INSERT,
                persist,
                new OrderEnricher(new ItemDaoImpl()),
                new PreparedSetter() {
                    @Override
                    public void setParameters(PreparedStatement ps) throws SQLException {
                        if(addParams.length > 0)
                        ps.setInt(1, (Integer) addParams[0]);
                        ps.setDate(2, new Date(System.currentTimeMillis()));
                    }
                });
    }

//    @Override
//    public Integer save(final Order persist) throws DataStoreException {
//        return null;
//    }

    @Override
    public List<Order> getAll() throws DataStoreException {
        return list(
                SQL_SELECT_ALL,
                new SimpleResultSetExtractor(new OrderRowMapper()),
                new OrderEnricher(new ItemDaoImpl()));

    }

    @Override
    public boolean exist(Integer integer) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Order getOrdersByTableId(final int id) throws DataStoreException {
        return executeParameter(
                SQL_SELECT_BY_TABLE_ID,
                new SimpleResultSetExtractor(new OrderRowMapper()),
                new OrderEnricher(
                        new ItemDaoImpl()),
                new PreparedSetter() {
                    @Override
                    public void setParameters(PreparedStatement ps) throws SQLException {
                        ps.setInt(1, id);
                    }
                }
        );

    }
}
